package cn.controller;

import cn.basic.BaseController;
import cn.dao.RoleSystemModelDAO;
import cn.dao.SystemModelDAO;
import cn.factory.DAOFactory;
import cn.model.TRoleSystemModel;
import cn.model.TSystemModel;
import cn.model.VRoleSystemModel;
import cn.model.VUserRole;
import cn.util.Expression;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 系统管理菜单业务控制类
 * @author Administrator8
 *
 */
@Controller
@RequestMapping(value="systemmodel")
public class SystemModelController extends BaseController {



	@RequestMapping(value="getsystemmodellist")
	public void getGetSystemModelList(Integer page,
									  Integer limit,
									  HttpServletRequest request,
									  HttpServletResponse response,
									  Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		RoleSystemModelDAO rosydao =DAOFactory.getRoleSystemModelDAO();
		List list = smdao.getTSystemModelListByPage(page,limit);
		List list2 = smdao.getTSystemModelListByOne();
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.count = list2.size();
			dt.msg = "查询成功，共查出" + dt.count + "条记录";
			dt.data = list;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	/**
	 * 条件查询一级的菜单
	 * @param page
	 * @param limit
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value="getallsystemmodel")
	public void getAllSystemModelList(
							  Integer page,
							  Integer limit,
							  String sysname,
							  String chinesename,
							  HttpServletRequest request,
							  HttpServletResponse response,
							  Model model) throws IOException{
		Expression exp = new Expression();
		if (sysname != null && !sysname.equals(""))
			exp.orLike("sysname", sysname, String.class);
		if (chinesename != null && !chinesename.equals("") )
			exp.andEqu("chinesename", chinesename, String.class);
		String wherecondition = exp.toString();

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		RoleSystemModelDAO rosydao =DAOFactory.getRoleSystemModelDAO();
		List list = smdao.getAllTSystemModelByTab(wherecondition, page, limit);
		int size = smdao.getAllTSystemModelCountByTab(wherecondition);
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功";
			dt.count = size;
			dt.data = list;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	/**
	 * 条件查询一级的菜单
	 * @param page
	 * @param limit
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value="getallsystemmodelone")
	public void getallsystemmodelone(
							 Integer page,
							 Integer limit,
							 String sysname,
							 HttpServletRequest request,
							 HttpServletResponse response,
							 Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		RoleSystemModelDAO rosydao =DAOFactory.getRoleSystemModelDAO();
		Expression exp = new Expression();
		List list =null;
		Integer size = null;
		if (sysname != null && !sysname.equals("")){
			exp.orLike("sysname", sysname, String.class);
			String wherecondition = exp.toString();
			list = smdao.getAllTSystemModelone(wherecondition, page, limit);
			size = smdao.getAllTSystemModelCountByTab(wherecondition);
			if(list!=null){
				dt.code = dt.FLAG_SUCC;
				dt.msg = "查询成功";
				dt.count = size;
				dt.data = list;
			}else{
				dt.code = dt.FLAG_FAIL;
				dt.msg = "查询失败";
			}
			closeout();
		}
		else {
			list = smdao.getTSystemModelListByPage(page,limit);
			List list2 = smdao.getTSystemModelListByOne();
			if(list!=null){
				dt.code = dt.FLAG_SUCC;
				dt.count = list2.size();
				dt.msg = "查询成功，共查出" + dt.count + "条记录";
				dt.data = list;
			}else{
				dt.code = dt.FLAG_FAIL;
				dt.msg = "查询失败";
			}
			closeout();
		}


	}
	/**
	 * 实现一个菜单的删除
	 * @throws IOException
	 */
	@RequestMapping(value="/deletesystemmodel")
	public void deleterole(
			               Integer id,
			               Integer sysid,
						   HttpServletRequest request,
						   HttpServletResponse response) throws IOException{
		RoleSystemModelDAO rolsdao = DAOFactory.getRoleSystemModelDAO();
		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		if (id!= null && !id.equals("")){
			if (rolsdao.deleteRoleSystemModel(id)){
				if (sysid!=null && !sysid.equals("")){
					smdao.deletesystemmole(sysid);
					dt.code = dt.FLAG_SUCC;
					dt.msg = "删除成功";
				}
			}else {
				dt.code = dt.FLAG_FAIL;
				dt.msg = "删除失败 ";
			}
		}else {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "删除失败 ";
		}

		closeout();
	}

	@RequestMapping(value="/getsystemmodellisttwo")
	public void getGetSystemModelListTwo(
					 Integer page,
					 Integer limit,
					 String oldid,
					 HttpServletRequest request,
					 HttpServletResponse response,
					 Model model) throws IOException{
		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();

		TSystemModel tm = smdao.getTSystemModelById(Integer.parseInt(oldid));
		List list = smdao.getTSystemModelListByPageTwo(Integer.parseInt(oldid),page,limit);
		List list2 = smdao.getTSystemModelListByOne(Integer.parseInt(oldid));
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.count = list2.size();
			dt.msg = "查询成功，共查出" + dt.count + "条记录";
			dt.data = list;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "无二级菜单";
		}
		closeout();
	}

	/**
	 * 实现根据session中保存的登录用户的roleid返回所有菜单列表的请求
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/getsystemmodelbyrole")
	public void getRoleSystemModelList(
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{
		VUserRole loginUser = (VUserRole)session.getAttribute("loginuser");

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		List list = smdao.getSystemModelByRole(loginUser.getRoleid());
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功，共查出" + list.size() + "条记录";
			dt.data = list;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	/**
	 * 授权部分，通过分页查询已有权限，再进行授权或取消授权
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/getRoleSystemModelByPage")
	public void getRoleSystemModelListByPage(
			Integer page,
			Integer limit,//一页多少条
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		RoleSystemModelDAO rosydao =DAOFactory.getRoleSystemModelDAO();
		List list = rosydao.getAllRoleSystemModelByPage(page, limit);
		int size = rosydao.getAllVRoleSystemModelPageAmount();
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功，共查出" + list.size() + "条记录";
			dt.data = list;
			dt.count = size;
			//System.out.println(JSON.toJSON(list));
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	/**
	 * 授权部分，通过roleid查询当前角色拥有哪些菜单权限
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/getRoleSystemModelByRoleid")
	public void getRoleSystemModelListByRoleid(
			Integer roleid,
			Integer limit,//一页多少条
			Integer page,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		List<VRoleSystemModel> list = smdao.getSystemModelByRoleid(roleid,page, limit);
		int size = smdao.getSystemModelAmountByRoleid(roleid);
		if(list!=null){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功，共查出" + list.size() + "条记录";
			dt.data = list;
			dt.count = size;
			//System.out.println(JSON.toJSON(list));
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	/**
	 * 实现根据传入TSystemModel添加一级菜单
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/addsystemmodel")
	public void addSystemModel(
			String name,
			String chinesename,
			String imageurl,
			String navurl,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		List list = smdao.getTSystemModelListByOne();
		TSystemModel tm = new TSystemModel();
		tm.setSysname(name);
		tm.setChinesename(chinesename);
		tm.setImageurl(imageurl);
		tm.setNavurl(navurl);
		tm.setDeepth(1);
		tm.setParentid(0);
		tm.setIsdelete("False");
		int count = list.size() + 1;
		tm.setDisplayorder(count);
		if(smdao.addSystemModel(tm)>0){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "添加成功";
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "添加失败";
		}
		closeout();
	}
	/*
	 * 实现根据传入TSystemModel添加二级菜单
	 * @param TSystemModel
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/addsystemmodeltwo")
	public void addSystemModelTwo(
			Integer id,
			String name,
			String chinesename,
			String imageurl,
			String navurl,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		if (smdao.isTSystemModelListByid(id)==true) {
			List list = smdao.getTSystemModelListByOne(id);
			TSystemModel tm = new TSystemModel();
			tm.setSysname(name);
			tm.setChinesename(chinesename);
			tm.setImageurl("&#xe602;");
			tm.setNavurl(navurl);
			tm.setDeepth(2);
			tm.setParentid(id);
			int count = list.size() + 1;
			tm.setDisplayorder(count);
			tm.setIsdelete("False");
			tm.setSystemname("PowerManage");
			System.out.println("添加");
			System.out.println(id);
			if(smdao.addSystemModel(tm)>0){
				dt.code = dt.FLAG_SUCC;
				dt.msg = "添加成功";
			}else{
				dt.code = dt.FLAG_FAIL;
				dt.msg = "添加失败";
			}
			closeout();
		}else {
			TSystemModel tm = smdao.getTSystemModelById(id);
			tm.setSysname(name);
			tm.setChinesename(chinesename);
			tm.setNavurl(navurl);
			tm.setSysid(tm.getSysid());
			tm.setImageurl(tm.getImageurl());
			tm.setDeepth(tm.getDeepth());
			tm.setParentid(tm.getParentid());
			tm.setDisplayorder(tm.getDisplayorder());
			tm.setIsdelete(tm.getIsdelete());
			tm.setSystemname(tm.getSystemname());
			tm.setAccesslevel(tm.getAccesslevel());
			System.out.println("修改");
			System.out.println(id);
			if(smdao.modifySystemModel(tm)){
				dt.code = dt.FLAG_SUCC;
				dt.msg = "修改成功";
				closeout();
			}else{
				dt.code = dt.FLAG_FAIL;
				dt.msg = "修改失败";
				closeout();
			}
			closeout();
		}

	}
	/*
	 * 实现根据传入roleid返回所有菜单列表的请求
	 * @param user
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="modifysystemmodel")
	public void ModifySystemModel(
			Integer sysid,
			String sysname,
			String chinesename,
			String imageurl,
			String navurl,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		List<TSystemModel> list = smdao.getTSystemModelListOneBySysId(sysid);
		TSystemModel tmd = list.get(0);

		TSystemModel tm = new TSystemModel();
		tm.setSysid(sysid);
		tm.setSysname(sysname);
		if(navurl==null){
			tm.setNavurl("");
		}else{
			tm.setNavurl("");
		}
		tm.setChinesename(chinesename);
		tm.setImageurl(imageurl);
		tm.setDeepth(1);
		tm.setParentid(0);
		tm.setSystemname("空");
		//int count = list.size() + 1;
		tm.setDisplayorder(tmd.getDisplayorder());
		tm.setIsdelete("False");
		if(smdao.modifySystemModel(tm)){
			dt.code = dt.FLAG_SUCC;
			dt.msg = "修改成功";
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "修改失败";
		}
		closeout();
	}

	@RequestMapping(value="/addrolepower")
	public void AddRolePower(
			String sysid,//模块编号
			String roleid,//角色编号
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		if (sysid == null || sysid.equals("") || roleid == null || roleid.equals("")) {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "权限添加失败";
		}else {
			//先查询当前菜单是否已经被当前角色所得)
			List<TRoleSystemModel> rolesysmidel = smdao.getTRoleSystemModelById(Integer.parseInt(roleid),Integer.parseInt(sysid));

			if (rolesysmidel.size() == 0) {//说明没有当前数据,直接添加
				TRoleSystemModel obj = new TRoleSystemModel();
				SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
				obj.setRoleid(Integer.parseInt(roleid));
				obj.setSysid(Integer.parseInt(sysid));
				obj.setIsedit(false);
				obj.setCreatedate(sdf.format(new Date()));

				if(smdao.addRoleSystemModel(obj) > 0){
					dt.code = dt.FLAG_SUCC;
					dt.msg = "添加成功";
				}else{
					dt.code = dt.FLAG_FAIL;
					dt.msg = "添加失败";
				}

			}else {//说明有当前数据那就是修改
				dt.code = dt.FLAG_FAIL;
				dt.msg = "该权限已经存在，请对其授权或者取消授权！";
			}
		}
		closeout();
	}

	@RequestMapping(value="/setrolepower")
	public void SetRolePower(
			String id,//模块编号
			String roleid,//角色编号
			String sysid,//菜单编号
			Boolean isedit,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

		SystemModelDAO  smdao = DAOFactory.getSystemModelDAO();
		if (id == null || id.equals("") || roleid == null || roleid.equals("") || sysid == null || sysid.equals("")) {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "权限操作失败";
		}else {

			VRoleSystemModel rolesys = smdao.getIsSysModelById(Integer.parseInt(id));
			if (rolesys != null) {//说明有这个数据
				if (isedit == true) {//那就只能是取消权限
					List<TRoleSystemModel> rolesysmidel = smdao.getTSystemModelByTid(Integer.parseInt(id));
					if (rolesysmidel.size() == 0) {//说明没有这条数据
						dt.code = dt.FLAG_FAIL;
						dt.msg = "请重新选择操作数据";
					}else {
						TRoleSystemModel tmodel = rolesysmidel.get(0);
						tmodel.setId(Integer.parseInt(id));
						tmodel.setRoleid(Integer.parseInt(roleid));
						tmodel.setSysid(Integer.parseInt(sysid));
						tmodel.setIsedit(false);
						tmodel.setCreatedate(tmodel.getCreatedate());
						if(smdao.modifyRoleSystemModel(tmodel)){
							dt.code = dt.FLAG_SUCC;
							dt.msg = "取消权限成功";
						}else{
							dt.code = dt.FLAG_FAIL;
							dt.msg = "取消权限失败";
						}
					}
				}else if (isedit == false) {//那就只能是设置权限
					List<TRoleSystemModel> rolesysmidel = smdao.getTSystemModelByTid(Integer.parseInt(id));
					if (rolesysmidel.size() == 0) {//说明没有这条数据
						dt.code = dt.FLAG_FAIL;
						dt.msg = "请重新选择操作数据";
					}else {
						TRoleSystemModel tmodel = rolesysmidel.get(0);
						tmodel.setId(Integer.parseInt(id));
						tmodel.setRoleid(Integer.parseInt(roleid));
						tmodel.setSysid(Integer.parseInt(sysid));
						tmodel.setIsedit(true);
						tmodel.setCreatedate(tmodel.getCreatedate());
						if(smdao.modifyRoleSystemModel(tmodel)){
							dt.code = dt.FLAG_SUCC;
							dt.msg = "设置权限成功";
						}else{
							dt.code = dt.FLAG_FAIL;
							dt.msg = "设置权限失败";
						}
					}
				}else {
					dt.code = dt.FLAG_FAIL;
					dt.msg = "权限操作失败";
				}
			}else {
				dt.code = dt.FLAG_FAIL;
				dt.msg = "请重新选择操作数据";
			}
		}
		closeout();
	}
}
